//
//  smOrderSearchController.m
//  shimaoHouse
//
//  Created by yst yst on 12-6-28.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "smOrderSearchController.h"
#import "smOrderInfoController.h"
#import "SUPApplication.h"
#import "smMboSmMboDB.h"
#import "SUPConnectionProperties.h"
#import "SUPLoginCredentials.h"
#import "SUPConnectionProfile.h"
#import "SUPObjectList.h"
#import "smMboSmTbOrder.h"
#import "HTTPImageView.h"

@interface smOrderSearchController () <UITableViewDelegate, UITableViewDataSource,UITextFieldDelegate,UISearchBarDelegate>

@property (nonatomic, strong) UINavigationController* navationCt;

@property (nonatomic, strong) UITableView* ystTableView;

@property (nonatomic, strong) UISearchBar* mySearBar;

@end

@implementation smOrderSearchController
@synthesize navationCt = _navationCt;
@synthesize ystTableView = _ystTableView;
@synthesize mySearBar = _mySearBar;
@synthesize orderList=_orderList;






- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    //[self.view setBackgroundColor:[UIColor blackColor]];
    
    UIBarButtonItem* tempRight = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editOrderList)];
    self.navigationItem.rightBarButtonItem = tempRight;
    
    
    
    [self.navigationController.navigationBar setHidden:YES];//隐藏title
    
    
    UIImage* titleImg = [UIImage imageNamed:@"orderlist_title.png"];    
    UIImageView *titleView =[[UIImageView alloc] initWithImage:titleImg];
    [titleView setFrame:CGRectMake(0, 0, self.view.bounds.size.width, 70)];
    
    [self.view addSubview:titleView];

    
    // tableVIEW
    
    self.ystTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, self.view.bounds.size.        width,self.view.bounds.size.height-150) ];//style:UITableViewStyleGrouped]  
    [self.ystTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.ystTableView setDelegate:self];
    [self.ystTableView setDataSource:self];
    
    
    
    self.mySearBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 70, self.view.bounds.size.width, 80)];  
    self.mySearBar.placeholder=@"输入关键字"; 
    self.mySearBar.delegate = self;
    self.ystTableView.tableHeaderView = self.mySearBar;  
    self.mySearBar.autocorrectionType = UITextAutocorrectionTypeNo;  
    self.mySearBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    UIImage* searchbarImg = [UIImage imageNamed:@"search.png"];
    [self.mySearBar setBackgroundImage:searchbarImg];
    
    [self.view addSubview: self.ystTableView];
    
    [self.tabBarController.view setHidden:NO];
}


//sup取数
- (void)viewWillAppear:(BOOL)animated {
    
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    [smMboSmMboDB beginSynchronize];
    SUPObjectList* list=[smMboSmTbOrder findAll];
    
    NSString* temp = [NSString stringWithFormat:@"list size is %d",[list size]];
    
    NSLog(temp,nil);
    
    if ([list length] > 0)
    {
        for (smMboSmTbOrder * oneRec in list)
        {
            [array addObject:oneRec];
        }
    }
    self.orderList = array;
    
    [self.ystTableView reloadData];
    [super viewWillAppear:animated]; 
}


-(void) editOrderList {
    
    if ([self.ystTableView isEditing]) {
        
        [self.navigationItem.rightBarButtonItem setTitle:@"编辑"];
        
        
        [self.ystTableView setEditing:NO animated:YES];
    }
    else {
        [self.navigationItem.rightBarButtonItem setTitle:@"完成"];    
        
        
        [self.ystTableView setEditing:YES animated:YES];
    }
    
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}






- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}// Default is 1 if not implemented


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 8;
    return [self.orderList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString* CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section, indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    } 
    
    for (UIView *v in [cell subviews]) {
        if ([v isKindOfClass:[UILabel class]]|[v isKindOfClass:[UIImage class]]) {
            [v removeFromSuperview];
        }
    }
    
    //右边加箭头
//    cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
    

    
  
//            
//    UIImage* tempImg = [UIImage imageNamed:@"img.jpg"];
//            
//    UIImageView* imgView = [[UIImageView alloc] initWithImage:tempImg];
    NSUInteger row = [indexPath row];
    HTTPImageView* imgView = [[HTTPImageView alloc] init];     
    [imgView setUrl:[NSString stringWithFormat:@"http://10.91.2.14:8080/upload_file/mobile/house%d.png",row+1]];
            
    [imgView setFrame:CGRectMake(10, 8, 200, 155)];
            
    [cell addSubview:imgView];
    
    // Set up the cell...
    smMboSmTbOrder *order = [self.orderList objectAtIndex:row];
    
    
    UIImage* seletedImg = [UIImage imageNamed:@"tableview_seletedBakground.png"];    
    UIImageView *seletedImgView =[[UIImageView alloc] initWithImage:seletedImg];
    [cell setSelectedBackgroundView:seletedImgView];
    
    UIImage* backImg = [UIImage imageNamed:@"tableview_background.png"];    
    UIImageView *backImgView =[[UIImageView alloc] initWithImage:backImg];
    [cell setBackgroundView:backImgView];
    
    
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(230, 30, 400, 30)];
     [tempLabel setBackgroundColor:[UIColor clearColor]];
     tempLabel.font = [UIFont systemFontOfSize:29];
     tempLabel.textColor=[UIColor colorWithRed:11/255.0 green:122/255.0 blue:182/255.0 alpha:1.0];
     tempLabel.text = [NSString stringWithFormat:@"订单编号：%@",[order id_]];
//     [tempLabel setText:@"订单编号：A100-1-30"];
     [cell addSubview:tempLabel];
    
    
    NSString * state=@"";
    if([[order orderState] isEqualToString:@"0"] ){
        state = @"删除";
    }else if([[order orderState]isEqualToString:@"1"]){
        state = @"正常";
    }
    UILabel* tempLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(230, 70, 200, 30)];
    [tempLabel1 setBackgroundColor:[UIColor clearColor]];
    tempLabel1.font = [UIFont systemFontOfSize:22];
    tempLabel1.textColor=[UIColor colorWithRed:122/255.0 green:122/255.0 blue:122/255.0 alpha:1.0];
    tempLabel1.text = [NSString stringWithFormat:@"当前状态：%@",state];
//    [tempLabel1 setText:@"当前状态：正常"];
    [cell addSubview:tempLabel1];
    
    UILabel* tempLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(230, 100, 200, 30)];
    [tempLabel2 setBackgroundColor:[UIColor clearColor]];
    tempLabel2.font = [UIFont systemFontOfSize:22];
    tempLabel2.textColor=[UIColor colorWithRed:122/255.0 green:122/255.0 blue:122/255.0 alpha:1.0];
    tempLabel2.text = [NSString stringWithFormat:@"客户名称：%@",[order custName]];
//    [tempLabel2 setText:@"客户名称：王的"];
    [cell addSubview:tempLabel2];
    
    return cell;
    
    
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView  
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle  
forRowAtIndexPath:(NSIndexPath *) indexPath  
{  
    NSLog(@"点击了删除");
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"信息" message:@"删除成功"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];	
//       [self.ystTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    } 
} 


//设置行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 171;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"订单管理";
}// fixed font style. use custom view (UILabel) if you want something different


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
     NSUInteger row = [indexPath row];
     smMboSmTbOrder *order = [self.orderList objectAtIndex:row];
    
     smOrderInfoController* orderInfoController = [[smOrderInfoController alloc] init];
    
    [orderInfoController setItems:NULL title:@"订单详情" something:1 ];
    [orderInfoController setItemsToInfo:order fl:@"update"];
    
    [self.navigationController pushViewController:orderInfoController animated:YES];    
}


//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//}
//
//// Data manipulation - reorder / moving support
//
//- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
//    
//}

//搜索框查询事件
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    SUPQuery *myquery = [SUPQuery getInstance];
    myquery.testCriteria = [SUPAttributeTest
                            match:@"custName" :searchBar.text];
    SUPObjectList* list=[smMboSmTbOrder findWithQuery:myquery];
    
    NSString* temp = [NSString stringWithFormat:@"list size is %d",[list size]];
    
    NSLog(temp,nil);
    
    if ([list length] > 0)
    {
        for (smMboSmTbOrder * oneRec in list)
        {
            [array addObject:oneRec];
        }
    }
    self.orderList = array;
    
    [self.ystTableView reloadData]; 
}





@end
